<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {checkEventStatus, checkStatus, listEvent} from "@/api/volunteer/event";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      allData: [
        {
        value:null,
        name:null
      }
      ],
      yData:[],
      allStatus:[],
      stateNum:[],
      event_status:[]
    }
  },
  mounted() {
    this.$nextTick(() => {
      listEvent().then((response)=>{
        for(let i=0;i<4;i++){
          this.stateNum[i]=response.rows.map((obj)=>{return obj.eventStatus}).filter((item)=>{return item==i}).length
        }
      checkEventStatus().then((response)=>{
      this.event_status=response.rows.map((obj)=>{return obj.stateName})
        for(let j=0;j<this.stateNum.length;j++){
          this.allData.push({value:this.stateNum[j],name:this.event_status[j]})
        }
        this.yData=this.allData.slice(1)
        this.initChart()
      })
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const option={
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: this.event_status
        },
        series: [
          {
            name: '当前状态活动个数',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.yData,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      }
      this.chart.setOption(option)
    }
  }
}
</script>
